package cn.yitun.chat.yitunserverimapicore.business.group.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupHeadDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupVo;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.group.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * Date 2019-01-20 20:49:52<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Component
@ActionMapping(value = ActionTypeConstant.GROUP_INFO_ACTION_PUSH)
public class GroupInfoAction {

	@Resource
	private GroupService groupService;
	@Resource
	private GroupCommonService groupCommonService;
	@Resource
	private GroupMemberService groupMemberService;
	@Resource
	private GroupHeadService groupHeadService;
	@Resource
	private GroupManageService groupManageService;

	/**
	 * Date 2019-01-26 14:36:56<br>
	 * Description 条件分页搜索群
	 * @author yitun
	 * @param groupQuery
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage queryGroupDataList(SocketSession socketSession, @JsonDefine("data") GroupDto groupQuery) {
		groupQuery.setCurrentUserId(socketSession.getKey());
		PageHelper.startPage(groupQuery.getCurrentPageNo(), groupQuery.getPageSize());
		List<GroupVo> groupList = groupService.queryGroupList(groupQuery);
		return new ResultMessage(R.param("page", new PageInfo<>(groupList)));
	}

	/**
	 * Date 2019-01-26 14:37:13<br>
	 * Description 群详情
	 * @author yitun
	 * @param socketSession
	 * @param groupQuery
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0002")
	public ResultMessage getGroup(SocketSession socketSession, @JsonDefine("data") GroupDto groupQuery) {
		return new ResultMessage(R.param("data", groupService.getVO(groupQuery.getId())));
	}

	/**
	 * Description 批量获取群信息 <br>
	 * Date 2019-05-05 08:39:53<br>
	 * @param groupQuery
	 * @return ResultMessage
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0003")
	public ResultMessage getGroupList(
			@JsonDefine("data") GroupDto groupQuery) {
		ResultMessage message = new ResultMessage();
		if (YitunCollectionUtil.isEmpty(groupQuery.getIds())) {
			message.addError(ExceptionEnum.LOSE_FILED);
			return message;
		}
		message.bodyPut("list", groupService.getGroupList(groupQuery.getIds()));
		return message;
	}

	/**
	 * Date 2019-01-26 14:39:42<br>
	 * Description 获取群头像信息
	 * @author yitun
	 * @param groupQuery
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0004")
	public ResultMessage getGroupHead(@JsonDefine("data") GroupDto groupQuery) {
		return new ResultMessage(R.param("head", groupHeadService.getGroupHeadByGroupId(groupQuery.getId())));
	}

	/**
	 * Date 2019-01-26 14:42:17<br>
	 * Description 创建群
	 * @author yitun
	 * @param head
	 * @param socketSession
	 * @param group
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0005")
	public Object addGroup(HeadImpl head, SocketSession socketSession,
						   @JsonDefine("data") GroupDto group) {
		if (NumUtil.isInValid(group.getCategoryId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		Long userId = socketSession.getKey();
		return groupService.add(head.getKey(), userId, group);
	}

	/**
	 * Date 2019-01-26 14:42:29<br>
	 * Description 修改群
	 * @author yitun
	 * @param head
	 * @param socketSession
	 * @param group
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0006")
	public Object updateGroupInfo(HeadImpl head, SocketSession socketSession,
							@JsonDefine("data") GroupDto group) {
		Long userId = socketSession.getKey();
		return groupService.updateGroupInfo(head.getKey(), userId, group);
	}

	/**
	 * 
	 * Date 2019-01-26 14:42:46<br>
	 * Description 修改头像
	 * 
	 * @author yitun
	 * @param groupHead
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0007")
	public Object uploadHead(SocketSession socketSession, @JsonDefine("data") GroupHeadDto groupHead) {
		Long userId = socketSession.getKey();
		return groupHeadService.uploadHead(userId, groupHead);
	}
}
